package my.project.sudoku;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Rect;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

public class PuzzleView extends View {

	private static final String TAG = "Sudoku";
	private final Game game;

	private float width;
	private float height;
	private int selX = 0;
	private int selY = 0;
	private final Rect selRect = new Rect();
	protected Paint background, dark, light, hilite;

	public PuzzleView(Context context) {
		super(context);
		background = new Paint();
		dark = new Paint();
		light = new Paint();
		hilite = new Paint();
		// TODO Auto-generated constructor stub
		this.game = (Game) context;
		setFocusable(true);
		setFocusableInTouchMode(true);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		// TODO Auto-generated method stub
		width = w / 9f;
		height = h / 9f;
		getRect(selX, selY, selRect);
		Log.d(TAG, "onSizeChanged: width " + width + ", height " + height);

		super.onSizeChanged(w, h, oldw, oldh);
	}

	private void getRect(int x, int y, Rect rect) {
		rect.set((int) (x * width), (int) (y * height),
				(int) (x * width + width), (int) (y * height + height));
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		background.setColor(getResources().getColor(R.color.puzzle_background));
		canvas.drawRect(0, 0, getWidth(), getHeight(), background);
		// draw the board
		// colors of grid lines
		dark.setColor(getResources().getColor(R.color.puzzle_dark));
		hilite.setColor(getResources().getColor(R.color.puzzle_hilite));
		light.setColor(getResources().getColor(R.color.puzzle_light));

		for (int i = 0; i < 9; i++) {
			canvas.drawLine(0, i * height, getWidth(), i * height, light);
			// create the lighter
			canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1,
					hilite);
			canvas.drawLine(i * width, 0, i * width, getHeight(), light);
			// create the lighter
			canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(),
					hilite);
		}
		// the most major lines
		for (int i = 0; i < 9; i++) {
			if (i % 3 != 0)
				continue;
			canvas.drawLine(0, i * height, getWidth(), i * height, dark);
			canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1,
					hilite);
			canvas.drawLine(i * width, 0, i * width, getHeight(), dark);
			canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(),
					hilite);
		}
		
		
		//draw number
		Paint foreground = new Paint(Paint.ANTI_ALIAS_FLAG); // research a little
		foreground.setColor(getResources().getColor(R.color.puzzle_foreground));
		foreground.setStyle(Paint.Style.FILL);
		foreground.setTextSize(height*0.75f);
		foreground.setTextScaleX(width/height);
		foreground.setTextAlign(Paint.Align.CENTER);
		
		FontMetrics fm = foreground.getFontMetrics();
		float x = width/2;
		float y = height/2 - (fm.ascent + fm.descent)/2; // research a little
		System.out.print(x);
		System.out.print(y);
		
		
		for(int i=0; i<9; i++)
		{
			for(int j=0; j<9; j++)
			{
				canvas.drawText("1", i * width + x, j * height + y, foreground);
			}
		}
		
		//show the selected rect
		Paint selected = new Paint();
		selected.setColor(getResources().getColor(R.color.puzzle_selected));
		canvas.drawRect(selRect, selected); // in the begining, the rect was drawn with mystic width and height
			//why is the lasted color-the color of selected- not cover the number ??? 
	}
	

	 private void select(int x, int y){
		 invalidate(selRect);
		 selX = Math.min(Math.max(x, 0), 8);
		 selY = Math.min(Math.max(y, 0), 8); //cach nay hay, ko phai xet nhieu
		 getRect(selX, selY, selRect);
		 invalidate(selRect); //about this stuff, can u explain what happen with ondraw method ?
	 }
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		Log.d(TAG, "onKeyDown: keycode=" + keyCode + ", event="
				+ event);
		switch(keyCode){
		case KeyEvent.KEYCODE_DPAD_UP:
			select(selX, selY - 1);
			break;
			case KeyEvent.KEYCODE_DPAD_DOWN:
			select(selX, selY + 1);
			break;
			case KeyEvent.KEYCODE_DPAD_LEFT:
			select(selX - 1, selY);
			break;
			case KeyEvent.KEYCODE_DPAD_RIGHT:
			select(selX + 1, selY);
			break;
			default:
			return super.onKeyDown(keyCode, event);
			}
		return true;
	}
	
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
	if (event.getAction() != MotionEvent.ACTION_DOWN)
	{
		return super.onTouchEvent(event);
	}
		
	select((int) (event.getX() / width),
	(int) (event.getY() / height));
	
	Log.d(TAG, "onTouchEvent: x " + selX + ", y " + selY);
	return true;
	}
	

	
	
	
}
